<?php


namespace app\common\lib\orm;


use think\facade\Env;

class ModelToTable
{
    static $me;

    private static function instance()
    {
        if (empty(self::$me)) {
            self::$me = new self();
        }
        return self::$me;
    }

    private static function uncamelize($camelCaps, $separator = '_')
    {
        return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
    }

    static function drop($class){
        $class_arr = explode("\\",$class);
        $model = array_pop($class_arr);
        $table = DB_PREFIX . self::uncamelize($model);
        $pre_sql = sprintf("DROP TABLE IF EXISTS `%s`;", $table);
        return $pre_sql;
    }

    static function create($class)
    {
        $class_arr = explode("\\",$class);
        $model = array_pop($class_arr);
        $class = "\\app\\common\\model\\" . $model;
        if (class_exists($class)) {
            $obj = new $class;
            if (isset($obj->columns)) {
                $table = DB_PREFIX . self::uncamelize($model);
                $columns = $obj->columns;
                return self::createTableSql($table,$columns);
            }
        } else {
            return false;
        }
    }

    ##
    ##
    ##类型
    ##
    ##
    use ModelColumns;
}